
const BASE_URL = "https://api-hmugo-web.itheima.net/api/public/v1/"
// const BASE_URL = "https://uinav.com/api/public/v1/"
// const BASE_URL = "https://api.zbztb.cn/api/public/v1/"

// "https://api-hmugo-web.itheima.net/"

 async function request({
	url, header={}, method='get', data={}
}) {
	uni.showLoading({title:'加载中'})
	
	
	let token = uni.getStorageSync('token')
	if(token) {
		header.Authorization = token
	};
	
	const res = await uni.request({
		url: (BASE_URL+url),
		header,
		method,
		data
	})
	
	uni.hideLoading()
	
	if (res.data.meta.status === 200) {
	    return {
	      msg: res.data.meta.msg,
	      data: res.data.message
	    }
	  } else {
			uni.showToast({
				title:res.data.meta.msg,
				
			})
			console.log(res.data)
	    return Promise.reject( new Error(res.errMsg) )
	  }
}
// 作为vue的插件
const requestPlugin = {
	install(Vue) {
		console.log('安装插件....')
		Vue.prototype.$request = request
	}
}
export default requestPlugin
